package progressBugBusiness;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/2/3.
 * x
 */
public class GetModule implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        ReturnJson returnJson = new ReturnJson();
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        JSONArray returnModule = new JSONArray();
        String action = request.getParameter("action");

        //根据拿到迭代版本
        //progresscase从progress表总获取版本列表
        if (Objects.equals("getModule", action)){
            String project = request.getParameter("project");
            String sql = "SELECT detail FROM progress WHERE progressname = '"+project+"'";
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            System.out.println(sqlReturnModel.getSelectReturnData().get(0).get(0));
            JSONArray jArr = JSON.parseArray(sqlReturnModel.getSelectReturnData().get(0).get(0));

            if (jArr == null || jArr.size() ==0){
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg("请在测试进度中添加模块");
                return returnJson.toString();
            }

            returnModule.add("");   //第一个空元素，页面可以默认一个空选项
            for (Object o: jArr){
                JSONArray one =(JSONArray)o;
                returnModule.add(one.getString(0));
            }

            returnJson.setResult(returnModule);
            returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }

        return returnJson.toString();
    }

}
